home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 05.zip / BS1 part 5 / SASC_6.0_Disk_7.adf / Source_And_Examples / examples / avg / avg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-30  |  3.9 KB  |  104 lines

  1. /*-----------------------------------------------------------------------*
  2.  * Copyright (c) 1989,1990,1991,1992 by SAS Institute Inc., Cary NC
  3.  *
  4.  * Name       : avg.c 
  5.  * Author     : Michael S. Whitcher
  6.  * Date       : 02June92
  7.  * Description: This is an example to demonstrate how to 
  8.  *              -- call assembly language routines from C
  9.  *              -- call assembly language routines from assembler
  10.  *              -- call into AmigaDos from assembler and C routines
  11.  *              -- call a registerized parameter link library routine 
  12.  *              -- setup an assembler routine to handle the passing 
  13.  *                   parameters on the stack and by register
  14.  * Sources    : avg.c avg2.c
  15.  *
  16.  * Buildline  : sc avg.c avg2.a link INCDIR=include: 
  17.  *              Writes the final executable to the name 'avg'.
  18.  *
  19.  * Scoptions  : Here is a list of options that you can use to vary how
  20.  *                 the example is built.  It gives you the opportunity 
  21.  *                 to see how the final executable changes as you change
  22.  *                 options.
  23.  *
  24.  *              DEBUG=SYMBOL  -- build with symbolic debugging information
  25.  *                This will allow you to use cpr to step through the program
  26.  *                in source mode and watch its execution.
  27.  *                                        
  28.  *              PARAMETERS=REGISTERS  -- pass the parameters to function calls
  29.  *                in register instead of storing them on the stack.
  30.  *
  31.  *              NOSTACKCHECK -- do not add code to check for stack overflows
  32.  *
  33.  *              NOSTANDARDIO -- causes the startup code not to open the default
  34.  *                I/O level 3 filehandles stdin,stdout,and stderr.  This will
  35.  *                drop the size of the executable.
  36.  * 
  37.  *              SMALLCODE  -- merge all code hunks together into one
  38.  *
  39.  *              SMALLDATA  -- merge all data hunks together into one
  40.  *
  41.  *              ADDSYMBOLS -- generate a H_SYMBOL hunk for use by cpr
  42.  *
  43.  *              STARTUP=cres  -- make the executable residentable
  44.  *              
  45.  *             
  46.  * Notes      : To build with registerized parameters add the 
  47.  *                parms=register option to the sc command line
  48.  *                or scoptions file.
  49.  *
  50.  *-----------------------------------------------------------------------*/
  51.  
  52. #include <dos/dos.h>
  53. #include <proto/dos.h>
  54. #include <pragmas/dos_pragmas.h>
  55. #include <string.h>
  56.  
  57. /* This is an assembler routine that calls AmigaDos to write a string to */
  58. /* an open AmigaDos file handle.                                         */
  59. extern int __asm prnts(register __d0 BPTR fh, register __a0 char *str);
  60.  
  61. /* This is an assembler routine that calculates the average of a set of  */
  62. /* numbers.  The routine is built so that it can be called with the      */
  63. /* parameters placed onto the stack or in register.                      */
  64. extern long avg(int argc, char **argv);
  65.  
  66. /* Because we have our own print routine and do not need standard I/O,   */
  67. /* we can compile this code with the 'NOSTDIO'.   It will produce much   */
  68. /* smaller code.                                                         */
  69. main(argc, argv)
  70. int argc;
  71. char **argv;
  72. {
  73.    BPTR fh;
  74.    long l;
  75.    char buf[16];
  76.    
  77.    /* Call into AmigaDos to get the name of the file handle that points */
  78.    /* to the output window from which we are executing.                 */
  79.    fh = Output();
  80.    
  81.    /* prnts() is an assembly routine that will write to the output      */
  82.    /* window.  avg() is an assembly routine that will compute the       */
  83.    /* average for us.                                                   */
  84.  
  85.    if (argc < 2)
  86.       prnts(fh, "Usage: average <int1> <int2> ...\n");
  87.    else
  88.    {
  89.       /* compute the average */
  90.       l = avg(argc-1, argv+1);
  91.       
  92.       /* convert the number to a string */
  93.       stcl_d(buf, l);
  94.       
  95.       /* output the average to the user */
  96.       prnts(fh, "average = ");
  97.       prnts(fh, buf);
  98.       prnts(fh, "\n");
  99.    }
  100.    
  101.    /* successful */
  102.    return 0;
  103. }
  104.